package com.message.Aliyun_ai_API.controller;
import com.base.annotation.SysLog;
import com.base.helper.Result;
import com.message.Aliyun_ai_API.service.WanXPictureService;
import com.message.Aliyun_ai_API.util.Aliyun_ai_imageUtil;
import com.message.Baidu_ai_API.util.FileSetUtil;
import com.message.Baidu_ai_API.util.FileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

@Api(tags = "阿里云api调用")
@RestController
@RequestMapping("/WanXPicture")
public class WanXPictureController {
    @Value("${external.resource.dir}")
    private String externalResourceDir;
    @Autowired
    private WanXPictureService wanXPictureService;
    @SysLog(action = "WanXPicture:TextToImage", value = "通义万象ai图像增强:文生图")
    @ApiOperation(value = "文生图",notes = "通义万象ai图像增强")
    @RequestMapping(value="TextToImage",method = {RequestMethod.POST})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userid", value = "id", paramType = "query"),//用户id，用于生成文件名
            @ApiImplicitParam(name = "text", value = "图片的文本描述", paramType = "query"),
            @ApiImplicitParam(name = "options", value = "需要图片的类型:1:默认值，由模型随机输出图像风格,2:摄影,3:写真,4:3D卡通,5:动画,6:油画,7:水彩,8:素描,9:中国画,10:扁平插画。", paramType = "query"),
            @ApiImplicitParam(name = "amount", value = "图片数量,尽量就一张吧，多张生成时间又久收费又贵，按张收费的", paramType = "query")})
    public Result<Object> TextToImage(@RequestParam("userid")Integer id,
                                      @RequestParam("text")String text,
                                      @RequestParam("options") Integer options,
                                      @RequestParam(value = "amount", defaultValue = "1") int amount) throws Exception {
        try{
            Map response=wanXPictureService.TextToImage(id,text,options,amount);
            return new Result<>(true,response);
        }catch(Exception e){
            return new Result<>(false,e.getMessage());
        }
    }
    @SysLog(action = "WanXPicture:getTextToImage", value = "通义万象ai图像增强:获取文生图结果")
    @ApiOperation(value = "获取文生图结果",notes = "通义万象ai图像增强")
    @RequestMapping(value="getTextToImage",method = {RequestMethod.POST})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userid", value = "id", paramType = "query"),//用户id，用于生成文件名
            @ApiImplicitParam(name = "Task_id", value = "任务id", paramType = "query")})
    public Result<Object> getTextToImage(@RequestParam("userid")Integer id,
                                      @RequestParam("Task_id")String Task_id) throws Exception {
        try{
            String imageUrl=wanXPictureService.getTextToImage(id,Task_id);
            return new Result<>(true,imageUrl);
        }catch(Exception e){
            return new Result<>(false,e.getMessage());
        }
    }
    @SysLog(action = "WanXPicture:AiTryon", value = "通义万象ai图像增强:ai试衣")
    @ApiOperation(value = "ai试衣",notes = "通义万象ai图像增强")
    @RequestMapping(value="AiTryon",method = {RequestMethod.POST})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userid", value = "用户id", paramType = "query"),//用户id，用于生成文件名
            @ApiImplicitParam(name = "top_garment", value = "上身装扮（必选，正面）", paramType = "query"),
            @ApiImplicitParam(name = "bottom_garment", value = "下身装扮，没输入则随机，或者如连衣裙等依附于上身的装扮该项置为空(正面)", paramType = "query"),
            @ApiImplicitParam(name = "person_image", value = "人(有且仅有一个人，且要求正面，不要拿东西，光线清晰)", paramType = "query")})
    public Result<Object> AiTryon(@RequestParam("userid")Integer userid,
                                  @RequestParam("top_garment")MultipartFile top_garment,
                                  @RequestParam(value = "bottom_garment",required = false)MultipartFile bottom_garment,
                                  @RequestParam("person_image") MultipartFile person_image) throws Exception {
        try{
            String top_garmentName=userid+"top_garment_url"+System.currentTimeMillis();
            String bottom_garmentName=userid+"bottom_garment_url"+System.currentTimeMillis();
            String person_imageName=userid+"person_image_url"+System.currentTimeMillis();
            FileSetUtil.saveFile(top_garment,externalResourceDir,top_garmentName);
            FileSetUtil.saveFile(bottom_garment,externalResourceDir,bottom_garmentName);
            FileSetUtil.saveFile(person_image,externalResourceDir,person_imageName);
           String top_garmentUrl= Aliyun_ai_imageUtil.getImageOfUrl(externalResourceDir+"/"+top_garmentName+".png",externalResourceDir);//映射url
            String bottom_garmentUrl= Aliyun_ai_imageUtil.getImageOfUrl(externalResourceDir+"/"+bottom_garmentName+".png",externalResourceDir);//映射url
            String person_imageUrl= Aliyun_ai_imageUtil.getImageOfUrl(externalResourceDir+"/"+person_imageName+".png",externalResourceDir);//映射url
            Map response=wanXPictureService.AiTryon(userid,top_garmentUrl,bottom_garmentUrl,person_imageUrl,top_garmentName,bottom_garmentName,person_imageName);
            return new Result<>(true,response);
        }catch(Exception e){
            return new Result<>(false,e.getMessage());
        }
    }
    @SysLog(action = "WanXPicture:getAiTryon", value = "通义万象ai图像增强:获取ai试衣结果")
    @ApiOperation(value = "获取ai试衣结果",notes = "通义万象ai图像增强")
    @RequestMapping(value="getAiTryon",method = {RequestMethod.POST})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userid", value = "id", paramType = "query"),//用户id，用于生成文件名
            @ApiImplicitParam(name = "Task_id", value = "任务id", paramType = "query")})
    public Result<Object> getAiTryon(@RequestParam("userid")Integer userid,
                                     @RequestParam("Task_id")String Task_id) throws Exception {
        try{
            String top_garmentName=userid+"top_garment_url"+System.currentTimeMillis()+".png";
            String bottom_garmentName=userid+"bottom_garment_url"+System.currentTimeMillis()+".png";
            String person_imageName=userid+"person_image_url"+System.currentTimeMillis()+".png";
            String imageUrl=wanXPictureService.getAiTryon(userid,Task_id,top_garmentName,bottom_garmentName,person_imageName);
            return new Result<>(true,imageUrl);
        }catch(Exception e){
            return new Result<>(false,e.getMessage());
        }
    }

    @SysLog(action = "WanXPicture:generation", value = "通义万象ai图像增强:人像风格重绘")
    @ApiOperation(value = "人像风格重绘",notes = "通义万象ai图像增强")
    @RequestMapping(value="generation",method = {RequestMethod.POST})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userid", value = "id", paramType = "query"),//用户id，用于生成文件名
            @ApiImplicitParam(name = "image", value = "图片（脸部清晰照片，人脸比例不宜过小）", paramType = "query"),
            @ApiImplicitParam(name = "style_index", value = "需要图片的类型:0 复古漫画,1 3D童话,2 二次元,3 小清新,4 未来科技,5 国画古风,6 将军百战,7 炫彩卡通,8 清雅国风,9 喜迎新年", paramType = "query"),
            @ApiImplicitParam(name = "style_ref_url", value = "参考图片。可不填", paramType = "query")})
    public Result<Object> generation(@RequestParam("userid")Integer userid,
                                     @RequestParam("image")MultipartFile image,
                                      @RequestParam("style_index") Integer style_index,
                                      @RequestParam(value = "style_ref_url",required = false) MultipartFile style_ref_url) throws Exception {
        try{
            String image_Name=userid+"image_url"+System.currentTimeMillis();
            FileSetUtil.saveFile(image,externalResourceDir,image_Name);
            String image_Url= Aliyun_ai_imageUtil.getImageOfUrl(externalResourceDir+"/"+image_Name+".png",externalResourceDir);//映射url
            Map response=wanXPictureService.generation(userid,image_Url,style_index,style_ref_url);
            return new Result<>(true,response);
        }catch(Exception e){
            return new Result<>(false,e.getMessage());
        }
    }

    @SysLog(action = "WanXPicture:getGeneration", value = "通义万象ai图像增强:获取人像风格重绘结果")
    @ApiOperation(value = "获取人像风格重绘结果",notes = "通义万象ai图像增强")
    @RequestMapping(value="getGeneration",method = {RequestMethod.POST})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userid", value = "id", paramType = "query"),//用户id，用于生成文件名
            @ApiImplicitParam(name = "Task_id", value = "任务id", paramType = "query")})
    public Result<Object> getGeneration(@RequestParam("userid")Integer userid,
                                     @RequestParam("Task_id")String Task_id) throws Exception {
        try{
            String image_Name=userid+"image_url"+System.currentTimeMillis()+".png";
            String imageUrl=wanXPictureService.getgeneration(userid,Task_id,image_Name);
            return new Result<>(true,imageUrl);
        }catch(Exception e){
            return new Result<>(false,e.getMessage());
        }
    }

    @SysLog(action = "WanXPicture:texture", value = "通义万象ai图像增强:创意文字生成")
    @ApiOperation(value = "创意文字生成",notes = "通义万象ai图像增强")
    @RequestMapping(value="texture",method = {RequestMethod.POST})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userid", value = "id", paramType = "query"),//用户id，用于生成文件名
            @ApiImplicitParam(name = "text_content", value = "输入文字内容", paramType = "query"),
            @ApiImplicitParam(name = "output_image_ratio", value = "想要的图片中文字的长宽比默认为1:1，可选的比例有：1:1,16:9, 9:16", paramType = "query"),
            @ApiImplicitParam(name = "font_name", value = "使用预置字体的编号，1:阿里妈妈东方大楷,2：阿里巴巴普惠体,3：阿里妈妈数黑体,4：钉钉进步体," +
                    "5：站酷酷黑体,6：站酷快乐体,7：站酷文艺体,8：站酷小薇LOGO体,9：站酷仓耳渔阳体,10：思源宋体,9：思源黑体,10：方正楷体", paramType = "query"),
            @ApiImplicitParam(name = "prompt", value = "提示词,默认为:墨水", paramType = "query"),
            @ApiImplicitParam(name = "texture_style", value = "风格,1:立体材质,2:场景融合,3:光影特效", paramType = "query")})
    public Result<Object> texture(@RequestParam("userid")Integer userid,
                                     @RequestParam("text_content")String text_content,
                                     @RequestParam("output_image_ratio") String output_image_ratio,
                                  @RequestParam("font_name")Integer font_name,
                                  @RequestParam("prompt") String prompt,
                                     @RequestParam("texture_style") Integer texture_style) throws Exception {
        try{
          Map response=wanXPictureService.texture(userid,text_content,output_image_ratio,font_name,prompt,texture_style);
            return new Result<>(true,response);
        }catch(Exception e){
            return new Result<>(false,e.getMessage());
        }
    }
    @SysLog(action = "WanXPicture:getTexture", value = "通义万象ai图像增强:获取创意文字生成")
    @ApiOperation(value = "获取创意文字生成",notes = "通义万象ai图像增强")
    @RequestMapping(value="getTexture",method = {RequestMethod.POST})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userid", value = "id", paramType = "query"),//用户id，用于生成文件名
            @ApiImplicitParam(name = "Task_id", value = "任务id", paramType = "query")})
    public Result<Object> getTexture(@RequestParam("userid")Integer userid,
                                        @RequestParam("Task_id")String Task_id) throws Exception {
        try{
            String imageUrl=wanXPictureService.getTexture(userid,Task_id);
            return new Result<>(true,imageUrl);
        }catch(Exception e){
            return new Result<>(false,e.getMessage());
        }
    }
}
